package eg.backend.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.datanucleus.jpa.annotations.Extension;

@Entity
@NamedQueries({
	@NamedQuery(name="Shop.getByKey", query="select t from eg.backend.domain.Shop t where t.id=:id"),
	@NamedQuery(name = "Shop.getGlobalShop", query = "select t from eg.backend.domain.GlobalShop t where t.name = :name "),
	@NamedQuery(name = "Shop.findNotUsedByNameAndOwner", query = "select g.name from eg.backend.domain.GlobalShop g where g.name.startsWith(:name) and " +
		"not exists (select t from eg.backend.domain.Shop t where t.usedBy = :owner and t.name = :name )"),
	@NamedQuery(name = "Shop.findNotUsedByOwner", query = "select g.name from eg.backend.domain.GlobalShop g where " +
		"not exists (select t from eg.backend.domain.Shop t where t.usedBy = :owner and t.name = g.name )"),
	@NamedQuery(name = "Shop.findByNameAndOwner", query = "select t.name from eg.backend.domain.Shop t where t.usedBy = :owner and t.name.startsWith(:name) "),
	@NamedQuery(name = "Shop.findByOwner", query = "select t.name from eg.backend.domain.Shop t where t.usedBy = :owner "),
	@NamedQuery(name="Shop.findAllGlobalShopNames", query="select g.name from eg.backend.domain.GlobalShop g "),
	
	@NamedQuery(name = "Shop.getByNameAndOwner", query = "select t from eg.backend.domain.Shop t where t.usedBy = :owner and t.globalShop = :globalShop "),
	@NamedQuery(name = "Shop.findUsedShops", query = "select t from eg.backend.domain.Shop t where t.usedBy = :owner order by t.usedTimes desc")
})
public class Shop {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	private String id;

	private String globalShop;
	private String name;//duplicate name of globalShop, space for time
	private String usedBy;
	
	private int usedTimes = 1;

	public Shop(String globalShop) {
		this.id = null;
		this.globalShop = globalShop;
	}
	
	public Shop(){}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getGlobalShop() {
		return globalShop;
	}

	public void setGlobalShop(String globalShop) {
		this.globalShop = globalShop;
	}

	public void incrementUsedTimes() {
		this.usedTimes++;
//		this.globalShop.incrementUsedTimes();
	}
	
	public void decreaseUsedTimes() {
		this.usedTimes--;
//		this.globalShop.decreaseUsedTimes();
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUsedBy() {
		return usedBy;
	}

	public void setUsedBy(String usedBy) {
		this.usedBy = usedBy;
	}

	public int getUsedTimes() {
		return usedTimes;
	}

	public void setUsedTimes(int usedTimes) {
		this.usedTimes = usedTimes;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!this.getClass().isInstance(obj)) {
			return false;
		}

		Shop shop = (Shop) obj;
		return new EqualsBuilder().append(this.getUsedBy(), shop.getUsedBy()).append(this.getGlobalShop(), shop.getGlobalShop()).append(this.getName(), shop.getName()).isEquals();	
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(this.getUsedBy())
		.append(this.getGlobalShop()).append(this.getName()).toHashCode();
	}
	
}
